home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Src / lconsole / channel.c next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  5.7 KB  |  276 lines

  1. /* channel.c: channel functions */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Src/lconsole/RCS/channel.c,v 6.0 1991/12/18 20:27:16 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Src/lconsole/RCS/channel.c,v 6.0 1991/12/18 20:27:16 jpo Rel $
  9.  *
  10.  * $Log: channel.c,v $
  11.  * Revision 6.0  1991/12/18  20:27:16  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16. #include "lconsole.h"
  17. #include "qmgr.h"
  18. #include "qmgr-int.h"
  19. #include <isode/cmd_srch.h>
  20. #ifdef  BSD42
  21. #include <sys/ioctl.h>
  22. #endif
  23.  
  24. #define plural(n,s)    ((n) == 1 ? "" : (s))
  25.  
  26. static CMD_TABLE *chanlist;
  27. static char *chantolist;
  28.  
  29.  
  30.  
  31. static void display_chan_info (cip)
  32. ChannelInfo *cip;
  33. {
  34.     printf ("%*sChannel %s (%s)\n", indent * 2, "",
  35.         cip -> channelname,
  36.         cip -> channeldescrip);
  37.     indent ++;
  38.     printf ("%*s%s message%s, ", indent *2, "",
  39.         datasize (cip -> numberMessages, ""),
  40.         plural(cip -> numberMessages,"s"));
  41.     printf ("%s delivery report%s, ",
  42.         datasize (cip->numberReports, ""),
  43.         plural(cip->numberReports,"s"));
  44.     printf ("%s MTA%s, ",
  45.         datasize(cip->numberMtas, ""), plural(cip->numberMtas, "s"));
  46.     printf ("%s data\n",
  47.         datasize (cip -> volumeMessages, ""));
  48.     if (cip -> numberActiveProcesses != 0) {
  49.         printf ("%*s%d process%s running", indent * 2, "",
  50.             cip -> numberActiveProcesses,
  51.             plural(cip->numberActiveProcesses, "es"));
  52.         if (cip -> maxprocs > 0)
  53.             printf ("(max %d)", cip -> maxprocs);
  54.         putchar ('\n');
  55.     }
  56.     display_status (cip -> status);
  57.     if (cip -> oldestMessage && (cip -> numberMessages > 0 ||
  58.                      cip -> numberReports > 0))
  59.         printf ("%*sOldest message is %s",
  60.             indent * 2, "",
  61.             ctime (&cip->oldestMessage));
  62.     indent --;
  63. }
  64.  
  65. static void display_all_chans (chans)
  66. ChannelInfo *chans;
  67. {
  68.     ChannelInfo *cip;
  69.     int width, w;
  70.     int count;
  71.     char **cpp;
  72.  
  73.     for (count = 0, cip = chans; cip; cip = cip -> next)
  74.         count ++;
  75.     cpp = (char **) smalloc (count * sizeof (char *));
  76.  
  77.     for (width = count = 0, cip = chans; cip; cip = cip -> next) {
  78.         char buf[BUFSIZ];
  79.         char c[2];
  80.  
  81.         c[1] = NULL;
  82.         c[0] = ':';
  83.  
  84.         if (cip -> numberActiveProcesses > 0)
  85.             c[0] = '*';
  86.         if (cip -> status -> enabled == 0)
  87.             c[0] = '!';
  88.         
  89.         if (cip -> numberReports != 0)
  90.             (void) sprintf (buf, "%s%s%d+%d",
  91.                     cip -> channelname, c,
  92.                     cip -> numberMessages,
  93.                     cip -> numberReports);
  94.         else if (cip -> numberMessages != 0)
  95.             (void) sprintf (buf, "%s%s%d",
  96.                     cip -> channelname, c,
  97.                     cip -> numberMessages);
  98.         else    (void) sprintf (buf, "%s%s", cip -> channelname,
  99.                     c[0] == ':' ? "" : c);
  100.         if ((w = strlen (buf)) > width)
  101.             width = w;
  102.         cpp[count++] = strdup (buf);
  103.     }
  104.     w = ncols (stdout) / (width+1);
  105.     for (count = 0, cip = chans; cip; cip = cip -> next) {
  106.         printf ("%-*s%s", width, cpp[count],
  107.             (count + 1) % w == 0 ? "\n" : " ");
  108.         free (cpp[count++]);
  109.     }
  110.     if (count % w != 0)
  111.         putchar ('\n');
  112.  
  113.     free ((char *)cpp);
  114. }
  115.  
  116. static void build_chanlist(chan)
  117. ChannelInfo *chan;
  118. {
  119.     ChannelInfo *cip;
  120.     int count;
  121.  
  122.     for (count = 0, cip = chan; cip; cip = cip -> next)
  123.         count ++;
  124.  
  125.     chanlist = (CMD_TABLE *) calloc ((unsigned)count + 1,
  126.                      sizeof *chanlist);
  127.  
  128.     for (count = 0, cip = chan; cip; cip = cip -> next) {
  129.         chanlist[count].cmd_key = strdup (cip -> channelname);
  130.         chanlist[count].cmd_value = count;
  131.         count ++;
  132.     }
  133.     chanlist[count].cmd_key = NULLCP;
  134.     chanlist[count].cmd_value = NOTOK;
  135. }
  136.  
  137. void free_chanlist ()
  138. {
  139.     CMD_TABLE *ct;
  140.  
  141.     if (chanlist == NULL)
  142.         return;
  143.  
  144.     for (ct = chanlist; ct -> cmd_key; ct++)
  145.         free(ct -> cmd_key);
  146.     free ((char *)chanlist);
  147.     chanlist = NULL;
  148. }
  149.  
  150. static int channel_noprint;
  151.  
  152. /* ARGSUSED */
  153. int chan_update (chan, id)
  154. ChannelInfo *chan;
  155. int id;
  156. {
  157.     ChannelInfo *cip;
  158.     int found;
  159.     char *cp;
  160.  
  161.     if (chan == NULL)
  162.         return NOTOK;
  163.  
  164.     if (chanlist == NULL)
  165.         build_chanlist(chan);
  166.  
  167.     if (channel_noprint) {
  168.         /* do nothing */;
  169.     }
  170.     else if (chantolist) {
  171.         found = 0;
  172.         if ((cp = re_comp (chantolist)) != NULLCP) {
  173.             advise (NULLCP, "Expression %s: %s",
  174.                 chantolist, cp);
  175.             free_ChannelInfo (chan);
  176.             return OK;
  177.         }
  178.         for (cip = chan; cip; cip = cip -> next)
  179.             if (re_exec (cip -> channelname) == 1) {
  180.                 display_chan_info (cip);
  181.                 found ++;
  182.             }
  183.         if (found == 0)
  184.             advise (NULLCP, "Channel %s not found", chantolist);
  185.     }
  186.     else if (chan -> next == NULL) 
  187.         display_chan_info (chan);
  188.     else
  189.         display_all_chans (chan);
  190.  
  191.     free_ChannelInfo(chan);
  192.     return OK;
  193. }
  194.  
  195. int f_channel (vec)
  196. char **vec;
  197. {
  198.     char errbuf[BUFSIZ];
  199.     int pid;
  200.  
  201.     if (*++vec == NULLCP) {
  202.         chantolist = NULLCP;
  203.     }
  204.     else {
  205.         chantolist = *vec;
  206.     }
  207.  
  208.     if (initiate_op (console_fd, operation_Qmgr_channelread,
  209.              NULLVP, &pid, chan_update, errbuf) == NOTOK) {
  210.         advise (NULLCP, "status operation failed: %s", errbuf);
  211.         return NOTOK;
  212.     }
  213.  
  214.     if (result_op (console_fd, &pid, errbuf) == NOTOK) {
  215.         advise (NULLCP, "result_op: %s", errbuf);
  216.         return NOTOK;
  217.     }
  218.     
  219.     return OK;
  220. }
  221.  
  222. char *fullchanname (str)
  223. char *str;
  224. {
  225.     char    *cp;
  226.     CMD_TABLE *tp, *fnd = NULL;
  227.     int ambig = 0;
  228.     int ch;
  229.     char *vec[2];
  230.  
  231.     if (chanlist == NULL) {
  232.         ch = channel_noprint, channel_noprint = 1;
  233.         vec[0] = "channel";
  234.         vec[1] = NULLCP;
  235.         f_channel (vec);
  236.         channel_noprint = ch;
  237.     }
  238.  
  239.     if ((cp = re_comp(str)) != NULLCP) {
  240.         advise (NULLCP, "Bad expression %s: %s", str, cp);
  241.         return NULLCP;
  242.     }
  243.  
  244.     for (tp = chanlist; tp -> cmd_key; tp ++)
  245.         if (lexequ (tp -> cmd_key, str) == 0) {
  246.             ambig = 0;
  247.             fnd = tp; /* exact match */
  248.             break;
  249.         }
  250.         else if (re_exec (tp -> cmd_key) == 1) {
  251.             if (fnd == NULL)
  252.                 fnd = tp;
  253.             else
  254.                 ambig = 1;
  255.         }
  256.     if (ambig) {
  257.         char *p, buf[BUFSIZ];
  258.         for (tp = chanlist, p = buf;
  259.              tp -> cmd_key; tp ++) {
  260.             if (re_exec (tp -> cmd_key) == 1) {
  261.                 (void) sprintf (p, "%s%s",
  262.                         p == buf ? "" : ", ",
  263.                         tp -> cmd_key);
  264.                 p += strlen(p);
  265.             }
  266.         }
  267.         advise (NULLCP, "%s is ambiguous, it matches %s", str, buf);
  268.         return NULLCP;
  269.     }
  270.  
  271.     if (fnd == NULL)
  272.         advise (NULLCP, "%s does not match any channel", str);
  273.     return fnd ? fnd -> cmd_key : NULLCP;
  274. }
  275.             
  276.